<template>
  <div class="app-container  bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view />
    </transition>
    <el-row v-if="isShow" class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-button v-if="queryMatch(16)" type="success" class="bg-green" icon="el-icon-circle-plus" @click="toModify('添加产品')">添加产品</el-button>
              <div class="flex-1" />
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @cell-click="bindCellClick" @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" min-width="80" header-align="center" align="center" sortable />
            <el-table-column prop="goods_pic" label="略缩图" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                <ws-img :src="ImageUrl + scope.row.goods_pic" class="image" alt="" style="width:86px;height:86px;"/>
              </template>
            </el-table-column>
            <el-table-column prop="goods_name" label="商品名称" min-width="200" header-align="center" align="center" />
            <el-table-column prop="goods_type_name" label="商品类型" min-width="120" header-align="center" align="center" sortable/>
            <!--<template slot-scope="scope">
                {{ scope.row.goods_type==0?'小程序':scope.row.goods_type==1?'腾讯云':scope.row.goods_type==2?'定制开发':scope.row.goods_type==3?'安全服务':'运维服务' }}
              </template>
            </el-table-column>-->
            <!--<el-table-column prop="price" label="价格" min-width="100" header-align="center" align="center" />-->
            <el-table-column prop="goods_top" label="置顶权重" min-width="120" header-align="center" align="center" sortable/>

            <el-table-column label="状态" min-width="80" header-align="center" align="center">
              <template slot-scope="scope">
                <div @click="queryMatch(17)?setState(scope.row):''">
                  <el-tag :type="scope.row.status==1?'danger':'success'">{{ scope.row.status==1?'已下架':'已上架' }}</el-tag>
                </div>
              </template>
            </el-table-column>
            <el-table-column v-if="queryMatch(17)||queryMatch(18)" label="操作" min-width="200" header-align="center" align="center">
              <template slot-scope="scope">
                <el-button v-if="queryMatch(17)" type="success" class="bg-green" size="small" icon="el-icon-edit" circle @click="toModify('编辑产品', scope.row)" />
                <el-button v-if="queryMatch(18)" type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange } from '../../../utils/table'

export default {
  data() {
    return {
      // ImageUrl: this.$store.getters.serverUrl,
ImageUrl: '',
      isShow: false,
      searchTerms: {
        searchValue: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '产品列表') {
        this.getData({ sortName: 'ID', sortOrder: 'ascending' })
        // this.getData()
        setTimeout(() => {
          this.isShow = true
        }, 500)
      } else {
        this.isShow = false
      }
    }
  },
  created() {
    if (this.$route.name === '产品列表') {
      this.isShow = true
      this.getData({ sortName: 'goods_top', sortOrder: 'ascending' })
      // this.getData()
    }
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    /**
       * 对应接口商品列表("goods/list"),
       * token  用户凭证  (必填)
       * page_no   当前页码
       * page_size   每页数量
       * sortName  排序字段名
       * sortOrder   排序方式
       * search  关键词
       * return: {
       *  "total":28,
       *  "rows":[{
       *     "id":6,
       *     "goods_name":"腾讯云服务器",
       *     "goods_pic":"/uploads/picture/20180209/151814392611406.jpeg",
       *     "goods_type":1,
       *     "status":0,
       *     "goods_top":0
       *   }]
       * }
       * */
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'goods/list'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 点击单元格进入详情
    bindCellClick(row, column, cell, event) {
      console.log(row, column, cell, event)
      if (column.property === 'goods_pic' || column.property === 'goods_name') {
        this.isShow = false
        const query = {}
        row && (query.id = row.id)
        this.$router.push({
          path: '/product/productList/details',
          query: query
        })
      }
    },

    /**
       * 设置当前产品的状态
       * row: 当前点击的行的数据
       */
    setState(row) {
      row.status = row.status === 0 ? 1 : 0
      const url = 'goods/ajax/' + row.id
      const data = { action: 'status', status: row.status }
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.$message.success('修改产品状态成功，状态：' + (row.status === 0 ? '已上架' : '已下架'))
      }).catch(e => {
        row.status = row.status === 0 ? 1 : 0
        this.$message.error('修改产品状态失败，请刷新或联系管理员')
      })
    },

    /**
       * 删除产品
       * row: 当前点击的行的数据
       */
    toDelete(row) {
      this.$confirm('此操作将把产品移入回收站, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'goods/delete/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData({ sortName: 'ID', sortOrder: 'ascending' })
        }).catch(e => {
          this.$message.error('删除失败,请刷新或联系管理员!')
        })
      })
    },

    /**
       * 跳转产品修改页面
       * title: 新增/修改
       * row: 当前点击的行的数据,为空时为新增
       */
    toModify(title, row) {
      this.isShow = false
      const query = { title: title }
      row && (query.id = row.id)
      this.$router.push({
        path: '/product/productList/modify',
        query: query
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    flex-wrap: wrap;
    > div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      width: 100%;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
.el-tag {
  cursor: pointer;
}
</style>
